const Sequelize = require('sequelize')
const { sequelize } = require('../init')
const moment = require('moment')
const nanoid = require('nanoid')

const room = sequelize.define('room', {
  id: {
    type: Sequelize.STRING,
    allowNull: false,
    primaryKey: true
  },
  creator: {
    type: Sequelize.STRING
  },
  // 1-普通房间 2-vip房间 3-双人房
  type: {
    type: Sequelize.STRING,
    allowNull: false
  },
  coin: {
    type: Sequelize.STRING,
    allowNull: false
  },
  duration: {
    type: Sequelize.INTEGER,
    allowNull: false
  },
  members: {
    type: Sequelize.STRING(1234),
    get() {
      return JSON.parse(this.getDataValue('members') || '[]')
    }
  },
  membersInfo: {
    type: Sequelize.TEXT,
    allowNull: false,
    get() {
      return JSON.parse(this.getDataValue('membersInfo') || '[]')
    }
  },
  // 成员人数
  memberNum: {
    type: Sequelize.STRING,
    allowNull: false
  },
  //匹配状态 1-匹配中 2-游戏中 3-游戏已结束 4-异常 积分退回？
  state: {
    type: Sequelize.STRING,
    allowNull: false,
    defaultValue: '1'
  },
  createdAt: {
    type: Sequelize.DATE,
    get() {
        return moment(this.getDataValue('createdAt')).format('YYYY-MM-DD HH:mm:ss');
    }
  },
  updatedAt: {
    type: Sequelize.DATE,
    get() {
        return moment(this.getDataValue('updatedAt')).format('YYYY-MM-DD HH:mm:ss');
    }
  }
}, {
    freezeTableName: true
})

module.exports = room